package dao.impl;

import entiey.Vip;
import entiey.Waiter;
import util.ResultSetCreateObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



import util.Instantiation;

import util.JDBCUtil;

import dao.VipDao;

public class VipDaoImpl implements VipDao,ResultSetCreateObject<Vip> {

	@Override
	/*添加会员*/
	public int add(Vip t) {
		int num = 0;
		String sql ="insert into vip values (null,?,?,?,now(),?,?,?,?,?,now(),?,?,?)";
		num = JDBCUtil.executeUpdate(sql,t.getCard(),t.getName(),t.getGender(),t.getTel(),t.getAddress(),t.getLevel(),t.getDiscount(),t.getIntegral(),t.getCostMoney(),t.getCostNum(),t.getRemark());
		return num;
	}

	@Override
	/*删除会员*/
	public int delete(int no) {
		String sql = "delete from vip where no=?";
		int num = 0 ;
		num = JDBCUtil.executeUpdate(sql, no);
		return num;
	}

	@Override
	/*修改会员*/
	public int update(Vip t) {
		System.out.println(t.getIntegral());
		String sql = "update vip set name=?,birthday=?,tel=?,address=?,level=?,discount=?,integral=?,handlecarddate=?,remark=? where card = ?";
		int num = 0;
		num = JDBCUtil.executeUpdate(sql, t.getName(),t.getBirthday(),t.getTel(),t.getAddress(),t.getLevel(),t.getDiscount(),t.getIntegral(),t.getHandleCardDate(),t.getRemark(),t.getCard());
		return num;
	}

	@Override
	/*查询所有会员信息*/
	public List<Vip> findAll() {
		List<Vip> list = new ArrayList<Vip>();
		String sql = "select * from vip";
		list = JDBCUtil.executeQuery(sql, this);		
		return list;
	}

	@Override
	/*通过卡号查询*/
	public Vip findById(int card) {
		String sql = "select *from vip where card = ?";
		List<Vip> list = null;
		list = JDBCUtil.executeQuery(sql, this, card);		
		Vip one = null;
		if(list.size()>0) {
			one =list.get(0);
		}
		return one;
	}


	@Override
	public Vip create(ResultSet rs) {
		Vip v = new Vip();
		try {
			v.setNo(rs.getInt("no"));
			v.setCard(rs.getInt("card"));
			v.setName(rs.getString("name"));
			v.setGender(rs.getString("gender"));
			v.setBirthday(rs.getDate("birthday"));
			v.setTel(rs.getString("tel"));
			v.setLevel(rs.getInt("Level"));
			v.setAddress(rs.getString("address"));
			v.setDiscount(rs.getString("discount"));
			v.setIntegral(rs.getInt("integral"));
			v.setHandleCardDate(rs.getDate("handlecarddate"));
			v.setCostMoney(rs.getString("CostMoney"));
			v.setCostNum(rs.getInt("CostNum"));
			v.setRemark(rs.getString("Remark"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return v;
	}


//通过卡号查询
	@Override
	public List<Vip> findVipByCard(int card) {
		String sql = "select *from vip where card = ?";
		List<Vip> list = null;
		list = JDBCUtil.executeQuery(sql, this, card);		
		Vip one = null;
		if(list.size()>0) {
			one =list.get(0);
		}
		return (List<Vip>) one;
	}

//通过姓名查询
	@Override
	public List<Vip> findByName(String name) {
		//return JDBCUtil.executeQuery("select * from vip where name like ?", this, "%"+name+"%");
		List<Vip> list = new ArrayList<Vip>();
		String sql = "select * from vip where name like ?";
		
		list = JDBCUtil.executeQuery(sql,this,"%"+name+"%");
		
		return list;
	
	

}
	
	

	@Override
	public List<Vip> findByTel(String tel) {
		List<Vip> list = JDBCUtil.executeQuery("select * from vip where tel like ?", this, "%"+tel+"%");
		return list;
	}

}

